﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Threading;
using System.Net.Sockets;


namespace jnxceq
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        
        private void MainForm_Load(object sender, EventArgs e)
        {

        }

        //开始监听　
        private void btn_start_Click(object sender, EventArgs e)
        {

            Thread t = new Thread(new ThreadStart(this.BeginListen));

            t.Start();
        }

        private void Start()
        {
            string m_serverIP = "127.0.0.1";
            int m_port = 9093;

            TcpListener m_Listener = null;
            IPAddress m_host;

            try
            {
                m_host = IPAddress.Parse(m_serverIP);
                m_Listener = new TcpListener(m_host, m_port);

                m_Listener.Start();

                while (true)
                {
                    TcpClient m_client = m_Listener.AcceptTcpClient();
                    ClientHandle m_handle = new ClientHandle();

                    m_handle.ClientSocket = m_client;

                    Thread m_clientThread = new Thread(new ThreadStart(m_handle.ResponseClient));
                    m_clientThread.Start();

                }

                m_Listener.Stop();

            }
            catch (Exception ex)
            {

                Console.WriteLine(ex.Message);
            }
        }


        private void BeginListen()
        {
            try
            {
                IPAddress ip = IPAddress.Parse("127.0.0.1");
                TcpListener listener = new TcpListener(ip, 9093);
                listener.Start();
                //Console.WriteLine("=====Socket文件服务开启=====");
 
                DoBeginAcceptTcpClient(listener);
 
 
            }
            catch (Exception ex)
            {
                Console.WriteLine("文件服务开启失败原因为:");
                Console.WriteLine(ex.Message);
            }
        }
 
        /// <summary>
        /// begin a new connection by asynchronous
        /// </summary>
        /// <param name="listener"></param>
        private void DoBeginAcceptTcpClient(TcpListener listener)
        {
            listener.BeginAcceptTcpClient(new AsyncCallback(this.DoAcceptTcpClientCallback), listener);
        }
        
 
 
        private void DoAcceptTcpClientCallback(IAsyncResult ar)
        {
 
            try
            {
                TcpListener listener = ar.AsyncState as TcpListener;
                TcpClient tcpClient = listener.EndAcceptTcpClient(ar);
 
                NetworkStream netStream = tcpClient.GetStream();
 
 
                string remoteIp = tcpClient.Client.RemoteEndPoint.ToString();
                //Console.WriteLine("Connect " + remoteIp);
          
                //add a new listener,so we can accept a new connection
                listener.BeginAcceptTcpClient(new AsyncCallback(this.DoAcceptTcpClientCallback), listener);
 
                string receive = string.Empty;
                //receive data 
                //netStream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(DoReceiveCallback), netStream);
            }
            catch (Exception ex)
            {
 
                Console.WriteLine(ex.Message);
            }
        }
 
 
        private void DoReceiveCallback(IAsyncResult ar)
        {
            try
            {
                NetworkStream netStream = ar.AsyncState as NetworkStream;
                if (netStream == null)
                {
                    return;
                }
                int count = 0;
                try
                {
                    count = netStream.EndRead(ar);
                    //netStream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(DoReceiveCallback), netStream);
                }
                catch { }
 
                //string msg = System.Text.Encoding.Default.GetString(buffer, 0, count);
                             // Console.WriteLine(msg);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }


    }
}
